﻿
using AppSocketService;
using BCGPSCommon;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Net;
using System.Net.Sockets; 
using System.Text;
using System;
using System.Collections.Generic;
using System.Threading;
using AppSocketControl.Relay;

namespace AppSocketControl
{
    public class Program
    {
        #region var
        //private static List<IGpsDataService> gpsDataServiceList = null;
        #endregion


        public static void Main(string[] args)
        {
            string vesion = "2.16 2021-5-12 14:15:36";
            Console.WriteLine("--------");
            Console.WriteLine("--------");
            Console.WriteLine("--------");
            Console.WriteLine("--------");
            Console.WriteLine($" in main *************************************************(版本:v{vesion})**************************************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
             
            new ConfigurationManager();
            SetUp();
            int gpsMax = int.Parse(ConfigurationManager.AppSettings["ThreadConfig:MaxGPSDevice"]);
            int oneThreadMax = int.Parse(ConfigurationManager.AppSettings["ThreadConfig:OneThreadTaskGPSDevice"]);

            Msg($"预设GPS终端设备总数:= {gpsMax}");
            Msg($"预设每个线程处理终端数量:= {oneThreadMax}");

            List<IGpsDataService> gpsDataServiceList = new List<IGpsDataService>();

            //UDPHandle udpHandle = new UDPHandle();
            //udpHandle.Init(GetIPAddress(), "53355");

            //UdpServer udpServer = new UdpServer(new IPEndPoint(IPAddress.Parse(GetIPAddress()), 53355));

            GpsDataService gpsDataService;
            ReadKafakaConsumer.gpsDataServiceList = new List<GpsDataService>();
            for (int i = 0; i < gpsMax / oneThreadMax; i++)
            { 
                gpsDataService = new GpsDataService(i);
                gpsDataService.IsIdel = true;
                //gpsDataServiceList.Add(gpsDataService);
                ReadKafakaConsumer.gpsDataServiceList.Add(gpsDataService);
                Msg($"启动线程编号:= {i+1}");
            }
            ReadKafakaConsumer.Start();


            RelayServer relayServer = new RelayServer();
            relayServer.Run();

            Console.ReadLine();
        }

        #region 读取配置文件
        private static void SetUp()
        {
            GeneralContainer.configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .Build();
        }
        #endregion

        #region 退出程序-终止线程
        //public static bool HandlerRoutine(int CtrlType)
        //{
        //    //switch (CtrlType)
        //    //{
        //    //    case 0:
        //    //        Console.WriteLine("0工具被强制关闭"); //Ctrl+C关闭 

        //    //        break;
        //    //    case 2:
        //    //        Console.WriteLine("2工具被强制关闭");//按控制台关闭按钮关闭 
        //    //        if (Program.gpsDataServiceList != null)
        //    //        {
        //    //            foreach (var item in Program.gpsDataServiceList)
        //    //            {
        //    //                item.IsThreadRun = false;
        //    //                item.thread.Join();
        //    //                item.thread = null;
        //    //            }
        //    //        }
        //    //        break;
        //    //}
        //    return false;
        //}
        #endregion

        #region 获得本机局域网IP地址
        private static string GetIPAddress()
        { 
            // 获得本机局域网IP地址  
            Console.WriteLine(Dns.GetHostName());
            IPAddress[] AddressList = null;

            
            foreach (var item in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if(item.AddressFamily == AddressFamily.InterNetwork)
                {
                    if(item.ToString() == "192.168.0.123")
                        AddressList = Dns.GetHostAddresses("192.168.0.123"); 
                    else
                        AddressList = Dns.GetHostAddresses("172.17.103.36");

                    break;
                }
            } 
            if (AddressList.Length < 1)
            {
                return "";
            }
            string ip = string.Empty;
            foreach (IPAddress item in AddressList)
            {
                Console.WriteLine(item.ToString() + ":\t" + item.AddressFamily);
                Console.WriteLine(item.ToString() + ":\t" + item.IsIPv4MappedToIPv6);
                Console.WriteLine(item.ToString() + ":\t" + item.IsIPv6LinkLocal);
                Console.WriteLine(item.ToString() + ":\t" + item.IsIPv6Multicast);
                Console.WriteLine(item.ToString() + ":\t" + item.IsIPv6SiteLocal);
                Console.WriteLine(item.ToString() + ":\t" + item.IsIPv6Teredo); 

                if (item.AddressFamily == AddressFamily.InterNetwork)
                {
                    ip = item.ToString();
                }
            }
            return ip;
        }
        #endregion
 
        static void Msg(string msg)
        {
            Console.WriteLine(DateTime.Now + "   " + msg);
        }
    }
}
